package com.ctshk.sapp.order.tour.controller;

import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.web.support.BaseController;
import com.ctshk.rpc.order.tour.dto.*;
import com.ctshk.rpc.order.tour.dto.custom.CustomOrderUpdateContactDTO;
import com.ctshk.rpc.order.tour.req.*;
import com.ctshk.rpc.order.tour.req.custom.CustomOrderUpdateContactReq;
import com.ctshk.rpc.order.tour.service.ISeriesOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * <p>
 * 系列团订单 前端控制器
 * </p>
 *
 * @author 谭响
 * @since 2021-01-18
 */
@Api(value = "订单", tags = "订单")
@RestController
@RequestMapping("/series/order")
public class SeriesOrderController extends BaseController {

    @DubboReference
    private ISeriesOrderService iSeriesOrderService;

    @ApiOperation(value = "下单", notes = "下单（查看团期-立即报名）")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "查看团期-立即报名", operDesc = "查看团期-立即报名")
    @PostMapping("/down")
    public Result<SeriesOrderDTO> down(@RequestBody @Valid SeriesOrderDownReq seriesOrderDownReq) {
        seriesOrderDownReq.setUserId(getCurrentUser(getRequest()).getId());
        return iSeriesOrderService.down(seriesOrderDownReq, getCurrentUser());
    }

    @ApiOperation(value = "确认订单", notes = "确认订单（立即确认-立即报名）")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "立即确认-立即报名", operDesc = "立即确认-立即报名")
    @PostMapping("/confirm")
    public Result<SeriesOrderDTO> confirm(@RequestBody @Valid SeriesOrderConfirmReq seriesOrderConfirmReq) {
        seriesOrderConfirmReq.setUserId(getCurrentUser(getRequest()).getId());
        return iSeriesOrderService.confirm(seriesOrderConfirmReq);
    }

    @ApiOperation(value = "追位", notes = "追位（查看团期-追位）")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "查看团期-追位", operDesc = "查看团期-追位")
    @PostMapping("/additional")
    public Result<SeriesOrderDTO> additional(@RequestBody @Valid SeriesOrderAdditionalReq seriesOrderAdditionalReq) {
        seriesOrderAdditionalReq.setUserId(getCurrentUser(getRequest()).getId());
        return iSeriesOrderService.additional(seriesOrderAdditionalReq);
    }

    @ApiOperation(value = "追位确认", notes = "追位确认（立即确认-追位）")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "立即确认-追位", operDesc = "立即确认-追位")
    @PostMapping("/additional/confirm")
    public Result additionalConfirm(@RequestBody @Valid SeriesOrderAdditionalConfirmReq seriesOrderAdditionalConfirmReq) {
        seriesOrderAdditionalConfirmReq.setUserId(getCurrentUser(getRequest()).getId());
        return iSeriesOrderService.additionalConfirm(seriesOrderAdditionalConfirmReq);
    }

    @ApiOperation(value = "追位审核", notes = "追位审核")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "追位审核", operDesc = "追位审核")
    @PostMapping("/additional/approval")
    public Result additionalApproval(@RequestBody @Valid SeriesOrderAdditionalApprovalReq req) {
        req.setUserId(getCurrentUser(getRequest()).getId());
        return iSeriesOrderService.additionalApproval(req, getCurrentUser());
    }

    @ApiOperation(value = "列表", notes = "列表（订单中心）")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "订单中心", operDesc = "订单中心")
    @PostMapping("/list")
    public Result<SeriesOrderListDTO> list(@RequestBody SeriesOrderListReq seriesOrderListReq) {
        return iSeriesOrderService.list(seriesOrderListReq, getCurrentUser(getRequest()));
    }

    @ApiOperation(value = "列表跨店", notes = "列表跨店（跨店訂單）")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "跨店訂單", operDesc = "跨店訂單")
    @PostMapping("/list/span")
    public Result<PageResponse<SeriesOrderListSpanDTO>> listSpan(@RequestBody SeriesOrderListSpanReq req) {
        req.setUserId(getCurrentUser(getRequest()).getId());
        return Result.success(iSeriesOrderService.listSpan(req));
    }

    @ApiOperation(value = "留位", notes = "留位（查看团期-留位）")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "查看团期-留位", operDesc = "查看团期-留位")
    @PostMapping("/keep")
    public Result<SeriesOrderDTO> keep(@RequestBody @Valid SeriesOrderKeepReq seriesOrderKeepReq) {
        seriesOrderKeepReq.setUserId(getCurrentUser(getRequest()).getId());
        return iSeriesOrderService.keep(seriesOrderKeepReq, getCurrentUser());
    }

    @ApiOperation(value = "留位确认", notes = "留位确认（立即确认-留位）")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "立即确认-留位", operDesc = "立即确认-留位")
    @PostMapping("/keep/confirm")
    public Result<SeriesOrderDTO> keepConfirm(@RequestBody @Valid SeriesOrderKeepConfirmReq seriesOrderKeepConfirmReq) {
        seriesOrderKeepConfirmReq.setUserId(getCurrentUser(getRequest()).getId());
        return iSeriesOrderService.keepConfirm(seriesOrderKeepConfirmReq);
    }

    @ApiOperation(value = "详情", notes = "详情（订单中心-详情）")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "订单中心-详情", operDesc = "订单中心-详情")
    @PostMapping("/detail")
    public Result<SeriesOrderDetailDTO> detail(@RequestBody @Valid SeriesOrderDetailReq seriesOrderListReq) {
        seriesOrderListReq.setUserId(getCurrentUser(getRequest()).getId());
        return iSeriesOrderService.detail(seriesOrderListReq);
    }

    @ApiOperation(value = "取消", notes = "取消（订单中心-取消订单）")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "订单中心-取消订单", operDesc = "订单中心-取消订单")
    @PostMapping("/cancel")
    public Result cancel(@RequestBody @Valid SeriesOrderCancelReq req) {
        req.setUserId(getCurrentUser(getRequest()).getId());
        return iSeriesOrderService.cancel(req);
    }

    @ApiOperation(value = "计算单房差", notes = "计算单房差（查看團期-立即报名/追位/留位-预算价格）")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.OTHER, operTitle = "查看團期-立即报名/追位/留位-预算价格", operDesc = "查看團期-立即报名/追位/留位-预算价格")
    @PostMapping("/calculation")
    public Result<SeriesOrderCalculationDTO> calculation(@RequestBody @Valid SeriesOrderCalculationReq req) {
        try {
            req.setUserId(getCurrentUser(getRequest()).getId());
            return iSeriesOrderService.calculation(req);
        }catch (BusinessException e){
            return Result.failed(e);
        }
    }

    @ApiOperation(value = "绑定会员", notes = "操作订单-使用会有信息-确认")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.OTHER, operTitle = "操作订单-使用会有信息-确认", operDesc = "操作订单-使用会有信息-确认")
    @PostMapping("/bind/member")
    public Result bindMember(@RequestBody @Valid SeriesOrderBindMemberReq req) {
        req.setUserId(getCurrentUser(getRequest()).getId());
        return iSeriesOrderService.bindMember(req);
    }

    @ApiOperation(value = "修改联络人信息", notes = "操作订单-修改联络人信息（叶冠峰）")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "操作订单-修改联络人信息", operDesc = "操作订单-修改联络人信息")
    @PostMapping("/update-contact")
    public Result<CustomOrderUpdateContactDTO> updateContact(@RequestBody @Valid CustomOrderUpdateContactReq req) {
        req.setUserId(getCurrentUser(getRequest()).getId());
        return iSeriesOrderService.updateContact(req);
    }

}
